package supremeragcalc.db;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.logging.Level;
import java.util.logging.Logger;

public class RagTxtInterpreter {

    BufferedReader reader;
    InputStreamReader streamReader;
    FileInputStream stream;
    File file;

    public RagTxtInterpreter(String filename) {
        file = new File(filename);
        run();
    }

    public void run() {
        try {
            stream = new FileInputStream(file);
            streamReader = new InputStreamReader(stream);
            reader = new BufferedReader(streamReader);

        } catch (FileNotFoundException ex) {
          ex.printStackTrace();
        }
    }

    public String readLine() {
        try {
            String line = reader.readLine();
            if(line == null) { return null; }

            line = line.trim();
            if(line.equals("")) { return null; }
            //if(isCommentLine(line)) { return null; }
            return line;

        } catch (IOException ex) {
            ex.printStackTrace();
        }

        return null;
    }

    public boolean isCommentLine(String line) {
        return line.startsWith("//");
    }

    public String[] splitComma(String line) {
        String[] split = line.split(",");
        for( int i = 0; i < split.length; i++ ) {
            split[i] = split[i].trim();
        }
        
        return split;
    }

    public String[] splitColon(String line) {
        String[] split = line.split(":");
        for( int i = 0; i < split.length; i++ ) {
            split[i] = split[i].trim();
        }

        return split;
    }

    public void close() {
        try {
            reader.close();
            streamReader.close();
            stream.close();
        } catch (IOException ex) {
            Logger.getLogger(RagTxtInterpreter.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
